{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Deep Neural Network in Keras"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In this notebook, we improve on our [intermediate neural net](https://github.com/the-deep-learners/deep-learning-illustrated/blob/master/notebooks/intermediate_net_in_keras.ipynb) by applying the theory we've covered since."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/the-deep-learners/deep-learning-illustrated/blob/master/notebooks/deep_net_in_keras.ipynb)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Load dependencies"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Using TensorFlow backend.\n"
     ]
    }
   ],
   "source": [
    "import keras\n",
    "from keras.datasets import mnist\n",
    "from keras.models import Sequential\n",
    "from keras.layers import Dense\n",
    "from keras.layers import Dropout # new!\n",
    "from keras.layers.normalization import BatchNormalization # new!\n",
    "from keras.optimizers import SGD"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Load data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "(X_train, y_train), (X_valid, y_valid) = mnist.load_data()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Preprocess data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "X_train = X_train.reshape(60000, 784).astype('float32')\n",
    "X_valid = X_valid.reshape(10000, 784).astype('float32')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "X_train /= 255\n",
    "X_valid /= 255"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "n_classes = 10\n",
    "y_train = keras.utils.to_categorical(y_train, n_classes)\n",
    "y_valid = keras.utils.to_categorical(y_valid, n_classes)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Design neural network architecture"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "model = Sequential()\n",
    "\n",
    "model.add(Dense(64, activation='relu', input_shape=(784,)))\n",
    "model.add(BatchNormalization())\n",
    "\n",
    "model.add(Dense(64, activation='relu'))\n",
    "model.add(BatchNormalization())\n",
    "\n",
    "model.add(Dense(64, activation='relu'))\n",
    "model.add(BatchNormalization())\n",
    "model.add(Dropout(0.2))\n",
    "\n",
    "model.add(Dense(10, activation='softmax'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "_________________________________________________________________\n",
      "Layer (type)                 Output Shape              Param #   \n",
      "=================================================================\n",
      "dense_1 (Dense)              (None, 64)                50240     \n",
      "_________________________________________________________________\n",
      "batch_normalization_1 (Batch (None, 64)                256       \n",
      "_________________________________________________________________\n",
      "dense_2 (Dense)              (None, 64)                4160      \n",
      "_________________________________________________________________\n",
      "batch_normalization_2 (Batch (None, 64)                256       \n",
      "_________________________________________________________________\n",
      "dense_3 (Dense)              (None, 64)                4160      \n",
      "_________________________________________________________________\n",
      "batch_normalization_3 (Batch (None, 64)                256       \n",
      "_________________________________________________________________\n",
      "dropout_1 (Dropout)          (None, 64)                0         \n",
      "_________________________________________________________________\n",
      "dense_4 (Dense)              (None, 10)                650       \n",
      "=================================================================\n",
      "Total params: 59,978\n",
      "Trainable params: 59,594\n",
      "Non-trainable params: 384\n",
      "_________________________________________________________________\n"
     ]
    }
   ],
   "source": [
    "model.summary()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Configure model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Train!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Train on 60000 samples, validate on 10000 samples\n",
      "Epoch 1/20\n",
      "60000/60000 [==============================] - 2s 34us/step - loss: 0.3717 - acc: 0.8896 - val_loss: 0.1522 - val_acc: 0.9518\n",
      "Epoch 2/20\n",
      "60000/60000 [==============================] - 1s 23us/step - loss: 0.1500 - acc: 0.9544 - val_loss: 0.1191 - val_acc: 0.9619\n",
      "Epoch 3/20\n",
      "60000/60000 [==============================] - 1s 23us/step - loss: 0.1121 - acc: 0.9655 - val_loss: 0.1055 - val_acc: 0.9670\n",
      "Epoch 4/20\n",
      "60000/60000 [==============================] - 1s 23us/step - loss: 0.0907 - acc: 0.9722 - val_loss: 0.0929 - val_acc: 0.9712\n",
      "Epoch 5/20\n",
      "60000/60000 [==============================] - 1s 22us/step - loss: 0.0754 - acc: 0.9769 - val_loss: 0.1001 - val_acc: 0.9698\n",
      "Epoch 6/20\n",
      "60000/60000 [==============================] - 1s 23us/step - loss: 0.0640 - acc: 0.9803 - val_loss: 0.0883 - val_acc: 0.9723\n",
      "Epoch 7/20\n",
      "60000/60000 [==============================] - 1s 23us/step - loss: 0.0578 - acc: 0.9814 - val_loss: 0.0838 - val_acc: 0.9745\n",
      "Epoch 8/20\n",
      "60000/60000 [==============================] - 1s 22us/step - loss: 0.0503 - acc: 0.9837 - val_loss: 0.0986 - val_acc: 0.9699\n",
      "Epoch 9/20\n",
      "60000/60000 [==============================] - 1s 23us/step - loss: 0.0455 - acc: 0.9852 - val_loss: 0.0821 - val_acc: 0.9755\n",
      "Epoch 10/20\n",
      "60000/60000 [==============================] - 1s 23us/step - loss: 0.0432 - acc: 0.9862 - val_loss: 0.0906 - val_acc: 0.9751\n",
      "Epoch 11/20\n",
      "60000/60000 [==============================] - 1s 23us/step - loss: 0.0401 - acc: 0.9869 - val_loss: 0.0863 - val_acc: 0.9754\n",
      "Epoch 12/20\n",
      "60000/60000 [==============================] - 1s 23us/step - loss: 0.0354 - acc: 0.9883 - val_loss: 0.0893 - val_acc: 0.9753\n",
      "Epoch 13/20\n",
      "60000/60000 [==============================] - 1s 23us/step - loss: 0.0327 - acc: 0.9889 - val_loss: 0.1022 - val_acc: 0.9718\n",
      "Epoch 14/20\n",
      "60000/60000 [==============================] - 1s 23us/step - loss: 0.0304 - acc: 0.9899 - val_loss: 0.0859 - val_acc: 0.9765\n",
      "Epoch 15/20\n",
      "60000/60000 [==============================] - 1s 23us/step - loss: 0.0288 - acc: 0.9906 - val_loss: 0.0865 - val_acc: 0.9787\n",
      "Epoch 16/20\n",
      "60000/60000 [==============================] - 1s 22us/step - loss: 0.0246 - acc: 0.9919 - val_loss: 0.0880 - val_acc: 0.9767\n",
      "Epoch 17/20\n",
      "60000/60000 [==============================] - 1s 22us/step - loss: 0.0254 - acc: 0.9916 - val_loss: 0.0885 - val_acc: 0.9754\n",
      "Epoch 18/20\n",
      "60000/60000 [==============================] - 1s 23us/step - loss: 0.0245 - acc: 0.9916 - val_loss: 0.0937 - val_acc: 0.9762\n",
      "Epoch 19/20\n",
      "60000/60000 [==============================] - 1s 22us/step - loss: 0.0228 - acc: 0.9922 - val_loss: 0.0885 - val_acc: 0.9774\n",
      "Epoch 20/20\n",
      "60000/60000 [==============================] - 1s 23us/step - loss: 0.0228 - acc: 0.9925 - val_loss: 0.0915 - val_acc: 0.9754\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<keras.callbacks.History at 0x7f9e4ccf22b0>"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.fit(X_train, y_train, batch_size=128, epochs=20, verbose=1, validation_data=(X_valid, y_valid))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
